package com.huli.controller;

import com.huli.domain.Orders;
import com.huli.param.OrdersByOwnerParam;
import com.huli.param.OrdersByUserParam;
import com.huli.param.OrdersCreateParam;
import com.huli.service.OrdersService;
import com.huli.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    @ApiOperation(value = "算价")
    @GetMapping("/orders/howMuch")
    public Result howMuch(OrdersCreateParam param) {
        BigDecimal bigDecimal = ordersService.howMuch(param);
        return Result.successResult(bigDecimal);
    }

    @ApiOperation(value = "新增订单 不直接调用 由user调用该接口")
    @PostMapping("/orders")
    public Result newOrders(OrdersCreateParam param) {
        ordersService.newOrders(param);
        return Result.successResult();
    }

    @ApiOperation(value = "取消订单(未支付情况下) 会按照时间比例扣除用户信誉积分")
    @DeleteMapping("/orders")
    public Result deleteOrders(int ordersId) {
        ordersService.delOrders(ordersId);
        return Result.successResult();
    }

    @ApiOperation(value = "获取用户所有订单")
    @GetMapping("/orders/user")
    public Result getUserOrders(OrdersByUserParam param) {
        List<Orders> userOrders = ordersService.getUserOrders(param);
        return Result.successResult(userOrders);
    }

    @ApiOperation(value = "获取位主所有订单")
    @GetMapping("/orders/owner")
    public Result getOwnerOrders(OrdersByOwnerParam param) {
        List<Orders> ownerOrders = ordersService.getOwnerOrders(param);
        return Result.successResult(ownerOrders);
    }

    @ApiOperation(value = "支付订单")
    @PutMapping("/orders/pay")
    public Result payOrders(int ordersId) {
        ordersService.payOrders(ordersId);
        return Result.successResult();
    }

    @ApiOperation(value = "退单(已支付)")
    @PutMapping("/orders/back")
    public Result backOrders(int ordersId) {
        ordersService.backOrders(ordersId);
        return Result.successResult();
    }

    @ApiOperation(value = "完成订单")
    @PutMapping("/orders/finish")
    public Result finishOrders(int ordersId) {
        ordersService.finishOrders(ordersId);
        return Result.successResult();
    }

    @ApiOperation(value = "通过车库获取其所有订单")
    @GetMapping("/orders/garage")
    public Result getByGarage(int garageId) {
        List<Orders> byGarage = ordersService.getByGarage(garageId);
        return Result.successResult(byGarage);
    }

    @ApiOperation(value = "通过车位获取订单")
    @GetMapping("/orders/carport/{carportId}")
    public Result getByCarportId(@PathVariable("carportId") int carportId) {
        List<Orders> byCarportId = ordersService.getByCarportId(carportId);
        return Result.successResult(byCarportId);
    }

    @ApiOperation(value = "查看订单的剩余支付时间 通过其他订单调用")
    @GetMapping("/orders/timeout/{ordersId}")
    public Result getOrdersTimeout(@PathVariable("ordersId") int ordersId) {
        Long ordersTimeout = ordersService.getOrdersTimeout(ordersId);
        return Result.successResult(ordersTimeout);
    }
}
